package com.jay.mypracticesdemo

import android.os.Bundle
import android.util.Log
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.ui.Modifier
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.viewModelScope
import com.jay.mvvm.MyViewModel
import com.jay.mypracticesdemo.ui.theme.MyPracticesDemoTheme
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.flowOf
import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.launch

class MainActivity : ComponentActivity() {
    companion object {
        const val TAG = "MainActivity"
    }

    lateinit var viewModel: MyViewModel
    lateinit var mainViewModel: MainViewModel

    //获取viewModel的方式
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        viewModel = ViewModelProvider(this, ViewModelProvider.NewInstanceFactory())[MyViewModel::class.java]
        setContent {
            MyPracticesDemoTheme {
                // A surface container using the 'background' color from the theme
//                GreetingPreview()
                Surface(
                    modifier = Modifier.fillMaxSize(),
                    color = MaterialTheme.colorScheme.background
                ) {
                    showModelList()
                }
            }
        }
        viewModel.liveData.observe(this) {
            Log.d(TAG, "onChange it=$it")
        }
        mainViewModel = ViewModelProvider(this, ViewModelProvider.NewInstanceFactory())[MainViewModel::class.java]
        val msf: MutableStateFlow<List<String>> = MutableStateFlow(emptyList())
        mainViewModel.liveData.observe(this) {
            Log.d(TAG, "onChange it=$it")
            msf.value = it
        }

        val flowOf = flowOf("1", "2", "3")
        GlobalScope.launch {
            flowOf
                .flowOn(Dispatchers.IO)
                .catch {
                    Log.e("MainViewModel", "Failed to get list of car models.")
                }
                .collect {
                    msf.value
                }
        }
    }

    class MainViewModel : ViewModel() {
        interface ApiClient {
            fun fetchModels(): Flow<List<String>>
        }

        val myFlow = object : ApiClient {
            override fun fetchModels(): Flow<List<String>> {
               return flow<List<String>> {

                }
            }

        }

        val liveData: MutableLiveData<List<String>> = MutableLiveData()

        fun loadDataFromWeb() {
            viewModelScope.launch {
                myFlow
                    .fetchModels()
                    .flowOn(Dispatchers.IO)
                    .catch {
                        Log.e("MainViewModel", "Failed to get list of car models.")
                    }
                    .collect {
                        liveData.postValue(it)
                    }
            }
        }
    }
}
